package cn.tedu.knows.search.controller;

import cn.tedu.knows.commons.vo.Topic;
import cn.tedu.knows.search.VO.QuestionVO;
import cn.tedu.knows.search.service.IQuestionService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class QuestionConsumer {
    // 获得业务逻辑层对象
    @Resource
    private IQuestionService questionService;

    // 这是kafka监听器监听指定的话题名称
    @KafkaListener(topics = Topic.QUESTION)
    public void questionReceive(ConsumerRecord<String, String> record) {
        // 将接收到的信息获取出来并转换为java对象
        String json = record.value();
        Gson gson = new Gson();
        QuestionVO questionVO = gson.fromJson(json, QuestionVO.class);
        // 调用业务逻辑层完成新增问题到ES的效果
        questionService.saveQuestion(questionVO);
        log.debug("成功的新增了问题到ES:{}", questionVO);
    }
}
